package sparql;

import java.util.ArrayList;
import java.util.Map;

import modex.Paragraphe;

public class SparqlClientExample {

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        SparqlClient sparqlClient = new SparqlClient("localhost:3030/space");

        String query = "ASK WHERE { ?s ?p ?o }";
        boolean serverIsUp = sparqlClient.ask(query);
        if (serverIsUp) {
            System.out.println("server is UP");

            nbPersonnesParPiece(sparqlClient);

            System.out.println("ajout d'une personne dans le bureau:");
            query = "PREFIX : <http://www.lamaisondumeurtre.fr#>\n"
                    + "PREFIX instances: <http://www.lamaisondumeurtre.fr/instances#>\n"
                    + "INSERT DATA\n"
                    + "{\n"
                    + "  instances:Bob :personneDansPiece instances:Bureau.\n"
                    + "}\n";
            sparqlClient.update(query);

            nbPersonnesParPiece(sparqlClient);

            System.out.println("suppression d'une personne du bureau:");
            query = "PREFIX : <http://www.lamaisondumeurtre.fr#>\n"
                    + "PREFIX instances: <http://www.lamaisondumeurtre.fr/instances#>\n"
                    + "DELETE DATA\n"
                    + "{\n"
                    + "  instances:Bob :personneDansPiece instances:Bureau.\n"
                    + "}\n";
            sparqlClient.update(query);
            
            nbPersonnesParPiece(sparqlClient);
            
            getSubTypes(sparqlClient, "montagne");
            
            getSynonymes(sparqlClient, "montagne");
            
            getInstances(sparqlClient, "montagne");
            
            
        } else {
            System.out.println("service is DOWN");
        }
    }
    
    private static void nbPersonnesParPiece(SparqlClient sparqlClient) {
        String query = "PREFIX : <http://www.lamaisondumeurtre.fr#>\n"
                    + "SELECT ?piece (COUNT(?personne) AS ?nbPers) WHERE\n"
                    + "{\n"
                    + "    ?personne :personneDansPiece ?piece.\n"
                    + "}\n"
                    + "GROUP BY ?piece\n";
            Iterable<Map<String, String>> results = sparqlClient.select(query);
            System.out.println("nombre de personnes par pièce:");
            for (Map<String, String> result : results) {
                System.out.println("\t"+result.get("piece") + " : " + result.get("nbPers"));
            }
    }
    
    public static ArrayList<String> getSynonymes(SparqlClient sparqlClient, String label) {
    	ArrayList<String> synonymes = new ArrayList<String>();
    	
        String query = "PREFIX : <http://ontologies.alwaysdata.net/space#>" +
        		"PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
        		"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
        		"PREFIX owl:  <http://www.w3.org/2002/07/owl#>" +
        		"PREFIX xsd:  <http://www.w3.org/2001/XMLSchema#>" +

				"SELECT ?syn WHERE" +
				"{\n" +
					"?myClass rdfs:label ?syn." +
					"?myClass rdfs:label \"" + label + "\"@fr." +
				"}\n";
        
        Iterable<Map<String, String>> results = sparqlClient.select(query);
        System.out.println("synonymes de " + label + " :");
        
        for (Map<String, String> result : results) {
        	synonymes.add(result.get("syn"));
            System.out.println("\t"+result.get("syn"));
        }
        return synonymes;
    }
    
    public static ArrayList<String> getSubTypes(SparqlClient sparqlClient, String label) {
    	ArrayList<String> subTypes = new ArrayList<String>();
    	
		String query = "PREFIX : <http://ontologies.alwaysdata.net/space#>" +
			"PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
			"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
			"PREFIX owl:  <http://www.w3.org/2002/07/owl#>" +
			"PREFIX xsd:  <http://www.w3.org/2001/XMLSchema#>" +
			
			"SELECT ?subClassLabel WHERE" +
			"{\n" +
				"?myClass rdfs:label \"" + label + "\"@fr." +
				"?subClass rdfs:subClassOf ?myClass." +
				"?subClass rdfs:label ?subClassLabel." +
			"}\n";
        
        Iterable<Map<String, String>> results = sparqlClient.select(query);
        System.out.println("sous types de " + label + " :");
        for (Map<String, String> result : results) {
        	subTypes.add(result.get("subClassLabel"));
            System.out.println("\t"+result.get("subClassLabel"));
        }
        return subTypes;
    }
    
    public static ArrayList<String> getInstances(SparqlClient sparqlClient, String label) {
    	ArrayList<String> instances = new ArrayList<String>();
    	
		String query = "PREFIX : <http://ontologies.alwaysdata.net/space#>" +
			"PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
			"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
			"PREFIX owl:  <http://www.w3.org/2002/07/owl#>" +
			"PREFIX xsd:  <http://www.w3.org/2001/XMLSchema#>" +
			
			"SELECT ?instanceLabel WHERE" +
			"{\n" +
			     "?Class rdfs:label \"" + label + "\"@fr." +
			     "?Instance rdf:type ?Class." +
			     "?Instance rdfs:label ?instanceLabel" +
			"}\n";
        
        Iterable<Map<String, String>> results = sparqlClient.select(query);
        System.out.println("instances de " + label + " :");
        for (Map<String, String> result : results) {
        	instances.add(result.get("instanceLabel"));
            System.out.println("\t"+result.get("instanceLabel"));
        }
        return instances;
    }
}
